package com.sy.common.service;

import com.sy.cloud.guess.model.Gokguessnum;
import com.sy.cloud.guess.model.Odds;
import com.sy.cloud.guess.model.Orderbuffer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by mac on 2017/11/27.
 */
@Transactional
@Slf4j
@Service
public class Algorithm implements IAlgorithm{
    /**
     * 100%控盘算出开奖号码
     * @param issueNo
     */

    @Resource
//    private IDbService dbService;
    private IBaseService baseService;
    @Resource
//    private ISetsService sets;
    private ITestService testService;
    @Override
    public Gokguessnum kongpancode(String issueNo ){
        // 大、小、单、双、顺子、三条
        Gokguessnum gokguessnumResult = new Gokguessnum();

        boolean ifshunzi = false;
        boolean ifsantiao = false;
        boolean ifbig = false; // false代表是小，true代表大
        boolean ifsignle = false; // false代表是双，true代表是单
        boolean ifshunzi1 = false;
        boolean ifsantiao1 = false;
        boolean ifbig1 = false; // false代表是小，true代表大
        boolean ifsignle1 = false; // false代表是双，true代表是单
        Orderbuffer orderbuffer = new Orderbuffer();


        // 获得下单金额

        StringBuilder selectSql0 = new StringBuilder();
        selectSql0.append(" From Orderbuffer ");
        List< Orderbuffer > list = baseService.listQuery( selectSql0.toString(), Orderbuffer.class );
        orderbuffer = (Orderbuffer)list.get(0);
        log.info(orderbuffer.toString());
        // 获得精确场的赔率
        Map<Integer,Integer> mapodd = testService.oddsSet("digit");

        // oddsList里面放的是 赔率*下单金额。就是一旦猜中，所有用户获得的金额

        List<Odds> oddsList = new ArrayList<Odds>();
        oddsList.add(new Odds(0,mapodd.get(10) * orderbuffer.getT0())); // 大
        oddsList.add(new Odds(1,mapodd.get(11) * orderbuffer.getT1())); // 单号
        oddsList.add(new Odds(2,mapodd.get(12) * orderbuffer.getT2())); // 顺子
        oddsList.add(new Odds(3,mapodd.get(13) * orderbuffer.getT3())); // 小
        oddsList.add(new Odds(4,mapodd.get(14) * orderbuffer.getT4())); // 双号
        oddsList.add(new Odds(5,mapodd.get(15) * orderbuffer.getT5())); // 三条

        Collections.sort(oddsList);
        for(Odds stu : oddsList){
            log.info("type:" + stu.getType() + ":value" + stu.getValue());
        }

        // 求是否是顺子和三条。概率都是1.61%
        int max=10000;
        Random random = new Random();
        int s = random.nextInt(max);

        if( s <= 161 ){
            // 是顺子
            gokguessnumResult.setShunzi(true);
            ifshunzi = true;
        }else if( s >= 161 && s < 322 ){
            // 是三条
            gokguessnumResult.setSan(false);
            ifsantiao = true;
        }

        // 判断是大还是小
        if( mapodd.get(10) * orderbuffer.getT0() > mapodd.get(13) * orderbuffer.getT3() ){

            // 需要的结果是小号，因为压大的大于压小的
            ifbig = false;
            gokguessnumResult.setBig(false);
            gokguessnumResult.setLittle( true );
        }else{
            ifbig = true;
            gokguessnumResult.setBig(true);
            gokguessnumResult.setLittle( false );
        }

        // 判断是双还是单
        if( mapodd.get(11) * orderbuffer.getT1() > mapodd.get(14) * orderbuffer.getT4() ){
            // 需要的结果是双号
            gokguessnumResult.setSingle( false );
            gokguessnumResult.setIfdouble( true );
            ifsignle = false;
        }else{
            gokguessnumResult.setSingle( true );
            gokguessnumResult.setIfdouble( false );
            ifsignle = true;
        }

        int[] a = new int[3];
        while( true ){


            int cou = 10;
            for( int i = 0; i <3; i++ ){
                a[i] = random.nextInt( cou );
            }

            if( (a[0] +  a[1] + a[2])%2 == 0 ){
                // 双
                ifsignle1 = false;
            }else{
                ifsignle1 = true;
            }
            if( (a[0] +  a[1] + a[2]) <= 13 ){
                // 小
                ifbig1 = false;
            }else{
                // 大
                ifbig1 = true;
            }

            if( (a[0]== a[1]) && (a[0]== a[2]) ){
                // 三条
                ifsantiao1 = true;
            }

            if( (a[0]+1==a[1] && a[1]+1==a[2]) || (a[0]-1==a[1] && a[1]-1==a[2] ) ){
                // 顺子
                ifshunzi1 = true;
            }

            if( (ifsignle == ifsignle1) && ( ifbig1 == ifbig ) && (ifsantiao1 == ifsantiao) &&  (ifshunzi1 == ifshunzi) ){
                // 匹配到合适的
                break;
            }

        }
        log.info("最终得到的三个数字");
        log.info(a[0]+""+a[1]+a[2] );
        int max1 = 259706;
        int min1 = 230123;
        int pre = random.nextInt(max1)%(max1-min1+1)+min1;
        String result = pre+""+a[0]+a[1]+a[2];

        gokguessnumResult.setOpencode( result );



        return gokguessnumResult;

    }



}
